//
//  LiveViewController.m
//  控制器解耦测试
//
//  Created by tianlong on 2018/11/8.
//  Copyright © 2018年 tianlong. All rights reserved.
//

#import "LiveViewController.h"
#import "LivePKBusiness.h"
#import "LiveCourseBusiness.h"
#import "LiveCommunicationCenter.h"

@interface LiveViewController ()
/** 存放各业务管理类 */
@property (strong, nonatomic) LivePKBusiness *pkBusiness;
@property (strong, nonatomic) LiveCourseBusiness *courseBusiness;
@property (nonatomic, strong) NSMutableArray *businessM;
@end

@implementation LiveViewController

#pragma mark - init
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        _communicationCenter = [[LiveCommunicationCenter alloc] init];
        _communicationCenter.liveVC = self;
        
        //注册业务类
        [self registeBussiness];
    }
    return self;
}

#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.communicationCenter live_viewDidLoad];
    
    //模拟课件作答完成去结果页面时通知LivePKBusiness去处理自己的业务
    [self.courseBusiness showResultPage:@"www.xes.com/gotoResultPage?gold=10&engine=20"];

    //模拟收到notice和topic notice
    [self receivedNotice:@" -------- notice -------- 消息"];
    [self receivedTopicNotice:@" --------- topic -------- 消息"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.communicationCenter live_viewWillAppear];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.communicationCenter live_viewDidAppear];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.communicationCenter live_viewWillDisappear];
    [self.communicationCenter didClickedBackButton:nil atIndex:1];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.communicationCenter live_viewDidDisappear];
}

#pragma mark - IRCNotice/IRCTopic
- (void)receivedNotice:(NSString *)msg {
    [self.communicationCenter live_receivedNotice:msg];
}

- (void)receivedTopicNotice:(NSString *)msg {
    [self.communicationCenter live_receivedTopicNotice:msg];
}

#pragma mark - 注册业务类、移除业务类
- (void)registeBussiness {
    //暂不支持业务类的懒加载，需要提前创建
    [self addBusinessFromArray:@[
                                 self.pkBusiness,
                                 self.courseBusiness
                                 ]];
}

- (void)addBusiness:(id<LiveCommunicationProtocol>)business {
    if (business && ![self.businessSource containsObject:business]) {
        [self.businessM addObject:business];
        business.liveVC = self;
    }
}

- (void)addBusinessFromArray:(NSArray<id<LiveCommunicationProtocol>> *)businessArr {
    __weak typeof(self)selfWeak = self;
    [businessArr enumerateObjectsUsingBlock:^(id<LiveCommunicationProtocol> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [selfWeak addBusiness:obj];
    }];
}

- (void)removeBusiness:(id<LiveCommunicationProtocol>)business {
    if (business) {
        business.liveVC = nil;
        [self.businessM removeObject:business];
    }
}

- (void)removeBusinessFromArray:(NSArray<id<LiveCommunicationProtocol>> *)businessArr {
    [businessArr enumerateObjectsUsingBlock:^(id<LiveCommunicationProtocol> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self removeBusiness:obj];
    }];
}


#pragma mark - lazyload
- (NSMutableArray *)businessM {
    if (!_businessM) {
        _businessM = [NSMutableArray array];
    }
    return _businessM;
}

- (NSArray *)businessSource {
    return [NSArray arrayWithArray:self.businessM];
}

- (LivePKBusiness *)pkBusiness {
    if (!_pkBusiness) {
        _pkBusiness = [[LivePKBusiness alloc] init];
    }
    return _pkBusiness;
}

- (LiveCourseBusiness *)courseBusiness {
    if (!_courseBusiness) {
        _courseBusiness = [[LiveCourseBusiness alloc] init];
    }
    return _courseBusiness;
}

#pragma mark - didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
